package informationGet.network;

import com.alibaba.fastjson.JSON;
import com.whu.zj.MyApplication;
import com.whu.zj.R;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.File;

import informationGet.dto.Points;

/**
 * 上传照片和测点坐标
 * Created by 凯 on 2015/11/12.
 */
public class UploadPhotoAndPoints {

    File file;  // 上传文件
    Upload upload;

    public UploadPhotoAndPoints(File file, Upload upload) {
        this.file = file;
        this.upload = upload;
    }

    // 上传
    public boolean upload() throws Exception {
        HttpClient httpClient = new DefaultHttpClient();
        String IP = MyApplication.share().getResources().getString(R.string.IP);
        String url = "http://" + IP + "/service/UploadPoints";
        HttpPost post = new HttpPost(url);
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

        builder.addTextBody("data", JSON.toJSONString(upload), ContentType.TEXT_PLAIN.withCharset("utf-8"));
        builder.addBinaryBody("file", file);
        HttpEntity entity = builder.build();
        post.setEntity(entity);

        HttpResponse response = httpClient.execute(post);

        if (response.getStatusLine().getStatusCode() == 200) {
            System.out.println("HttpUrlUpload(uploadFile)文件上传成功！！！");
            return true;
        }
        System.out.println("HttpUrlUpload(uploadFile)文件上传失败！！！");
        return false;
    }


    public static class Upload {
        Points points;

        public Points getPoints() {
            return points;
        }

        public void setPoints(Points points) {
            this.points = points;
        }

    }

}
